package com.feishi.core.media.javacv;



import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgcodecs.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;
/**
 *
 *
 * */
public class CompareImgByHist {
    public static void main(String[] args) {
        Mat orgImg = imread("/home/caixq/Desktop/dd.png");

        Mat img = imread("/home/caixq/Desktop/cc.png");

        System.out.println(compareImgByHist(img, orgImg));


    }
    private static boolean compareImgByHist(Mat img, Mat orgImg)

    {

        Mat tmpImg=new Mat();

        resize(img, tmpImg, orgImg.size());

//        imshow("Img1", img);
//
//        imshow("tmpImg", tmpImg);
//
//        imshow("orgImg", orgImg);

//HSV颜色特征模型(色调H,饱和度S，亮度V)

        cvtColor(tmpImg, tmpImg, COLOR_BGR2HSV);

        cvtColor(orgImg, orgImg, COLOR_BGR2HSV);

//直方图尺寸设置

//一个灰度值可以设定一个bins，256个灰度值就可以设定256个bins

//对应HSV格式,构建二维直方图

//每个维度的直方图灰度值划分为256块进行统计，也可以使用其他值

        int hBins = 256, sBins = 256;

        int histSize[] = { hBins,sBins };

//H:0~180, S:0~255,V:0~255

//H色调取值范围

        float hRanges[] = { 0,180 };

//S饱和度取值范围

        float sRanges[] = { 0,255 };

        float ranges[] = { hRanges[0],hRanges[1],sRanges[0],sRanges[1] };

        int channels[] = { 0,1 };//二维直方图

        Mat hist1=new Mat()
        , hist2 = new Mat();

        calcHist(tmpImg, 1, channels, new Mat(), hist1,2,histSize, ranges, true, false);

        normalize(hist1, hist1, 0, 1, NORM_MINMAX, -1,new  Mat());

        calcHist(orgImg, 1, channels, new Mat(), hist2, 2, histSize, ranges, true, false);

        normalize(hist2, hist2, 0, 1, NORM_MINMAX, -1,new Mat());

        double similarityValue = compareHist(hist1, hist2, CV_COMP_CORREL);

        System.out.println(similarityValue);
        if (similarityValue >= 0.85)

        {

            return true;

        }

        return false;

    }


}
